import { AsyncStorage } from 'react-native';
import { kAuthorization, kContentType } from "../constant";
import Request from "../utils/Request";
import constant from "../utils/constant";
import RouterServer from "../utils/RouterServer";
const { Toast } = require('antd-mobile');
const STORAGE_KEY = "STORAGE_KEY_place";
export default {
    namespace: 'place',
    state: {
        questionTypeList: null
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        *saveToStorage({ payload }, { call }) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload));
        },
        *restoreFromStorage({ payload }, { call, put, select }) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: Object.assign({}, JSON.parse(data))
            });
        },
        *registeGuest({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            let params = {};
            params[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            params[kContentType] = 'application/json';
            params['params'] = Object.assign({}, payload);
            const result = yield call(Request.post, Request.getApi().guests, params);
            if (result) {
                Toast.success('登记成功', constant.ToastDuration);
                yield call(RouterServer.pop);
            }
        },
        *fetchQuestionTypeList({ payload }, { call, put, select }) {
            if (!payload)
                payload = { pi: 1, ps: 100 };
            const auth = yield select(state => state.auth);
            payload[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            const questionTypeList = yield call(Request.get, Request.getApi().questionType, payload);
            if (questionTypeList) {
                yield put({ type: 'save', payload: { questionTypeList } });
            }
        },
        *reportQuestion({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            let params = {};
            params[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            params[kContentType] = 'application/json';
            params['params'] = Object.assign({}, payload);
            const from = payload.from;
            delete payload.from;
            const result = yield call(Request.post, Request.getApi().support, params);
            if (result) {
                Toast.success('提交成功', constant.ToastDuration);
                yield call(RouterServer.pop);
            }
        },
        *fetchAreaUser({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            if (!payload)
                payload = {};
            payload[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            const result = yield call(Request.get, Request.getApi().areaUser + `/${auth.userInfo.company.area.id}`, payload);
        },
    },
    subscriptions: {
        setup({ dispatch }) {
        }
    },
};
